package com.goodlearning.gloriamobile.activity;

import java.util.ArrayList;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
//import com.google.android.gms.maps.CameraUpdateFactory;
import com.goodlearning.gloriamobile.R;
import com.goodlearning.gloriamobile.commons.Utils;
import com.goodlearning.gloriamobile.dto.GetMapPositionListDto;
import com.goodlearning.gloriamobile.net.JsonServiceRequester;
import com.goodlearning.gloriamobile.net.JsonWrapper;
//import com.google.android.gms.maps.GoogleMap;
//import com.google.android.gms.maps.SupportMapFragment;
//import com.google.android.gms.maps.model.BitmapDescriptorFactory;
//import com.google.android.gms.maps.model.CameraPosition;
//import com.google.android.gms.maps.model.LatLng;
//import com.google.android.gms.maps.model.MarkerOptions;
//import com.google.android.gms.maps.model.PolygonOptions;
import com.goodlearning.gloriamobile.net.JsonRequestThread.INetSuccessCallback;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.View;

/**
 * 학교 시설 위치에 대한 Map View 출력 처리
 * 
 * @author rapty54
 * 
 */
public class MoreGloriaMapPosition extends BaseActivity implements LocationListener{
	
//	GoogleMap map;
	
	ArrayList<GetMapPositionListDto> mapPositionListDtos;
	
	int resouceIdx[]={R.drawable.btn_mappoint,R.drawable.btn_mappoint_02,R.drawable.btn_mappoint_03
			,R.drawable.btn_mappoint_04,R.drawable.btn_mappoint_05,R.drawable.btn_mappoint_06};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gloria_map_body_layout);
		init();

	}

	public void init() {
		baseTitleLeftBtn.setVisibility(View.VISIBLE);
		baseTitleLeftBtn.setTextColor(getResources().getColor(R.color.more_third_text_change));
		baseTitleLeftBtn.setText(R.string.btn_char1_shift_1);
		baseTitleLeftBtn.setTextSize(20.0f);
		baseTitleTv.setText("학교시설위치");
		baseTitleRightBtn.setVisibility(View.INVISIBLE);
		requestMapPosition(MoreGloriaMapPosition.this);
	}
	
	/**
	 *  Map 각 학교 위치 요청 응답값 Parameter 값 없고 모두 한번에 불러들임
	 * @param context
	 */
	public void requestMapPosition(Context context){
		JsonServiceRequester.requestGetOrgList(context, new INetSuccessCallback() {
			
			@Override
			public void sendCallback(JsonWrapper jsonWrapper) {
				
				JSONArray jsonArray=jsonWrapper.getRootArray();
				
				mapPositionListDtos=new ArrayList<GetMapPositionListDto>();
				
				for(int i=0;i<jsonArray.size();i++){
					
					Utils.log("========="+jsonArray.size()+"=======");
					
					JSONObject jsonObject = (JSONObject) jsonArray.get(i);
					
					GetMapPositionListDto getMapPositionListDto=new GetMapPositionListDto();
					
					getMapPositionListDto.setOrgImgKey(Utils.convertObjToString(jsonObject.get("orgImgKey")));
					getMapPositionListDto.setOrgLong(Utils.convertObjToString(jsonObject.get("orgLong")));
					getMapPositionListDto.setOrgLati(Utils.convertObjToString(jsonObject.get("orgLati")));
					getMapPositionListDto.setIdx(Utils.convertObjToString(jsonObject.get("idx")));
					getMapPositionListDto.setOrgName(Utils.convertObjToString(jsonObject.get("orgName")));
					
					mapPositionListDtos.add(getMapPositionListDto);
					
					getEachPosition(Double.valueOf(Utils.convertObjToString(jsonObject.get("orgLati")))
							,Double.valueOf(Utils.convertObjToString(jsonObject.get("orgLong")))
							,Utils.convertObjToString(jsonObject.get("orgName"))
							,resouceIdx
							,Utils.convertObjToString(jsonObject.get("orgImgKey")));
				}
			}
		});
	}
	
	/**
	 * 지도에서 가져올 위치를 선정한다.
	 */
	public void getEachPosition(double latitude,double longtitude,String message,int[]resource,String ImgKey){
		
		int minLatitude = Integer.MAX_VALUE;
		int minLongtitude =Integer.MAX_VALUE;
		int maxLatitude =0;
		int maxLongtitude =0;
		
		if(latitude!=0 && longtitude!=0 && message!=null){
			
//			PolygonOptions polygonOptions=new PolygonOptions();
			int lat = (int) (latitude  * 1E6); 
			int lon = (int) ( longtitude  * 1E6);
			
			minLatitude = lat < minLatitude ? lat : minLatitude;
			minLongtitude = lon < minLongtitude ? lon : minLongtitude;
			maxLatitude = maxLatitude < lat ? lat : maxLatitude;
			maxLongtitude = maxLongtitude < lon ? lon : maxLongtitude;
			
//			polygonOptions.add(new LatLng(latitude, longtitude));
//			map.addPolygon(polygonOptions);
			
			int resourcePic = 0;
			for(int i=0;i<resource.length;i++){
				int resourceKeys=Integer.valueOf(ImgKey);
				switch (resourceKeys) {
				case 1:
					resourcePic=resource[0];
					break;
				case 2:
					resourcePic=resource[1];
					break;
				case 3:
					resourcePic=resource[2];
					break;
				case 4:
					resourcePic=resource[3];
					break;
				case 5:
					resourcePic=resource[4];
					break;
				case 6:
					resourcePic=resource[5];
					break;	
				}
			}
			Utils.log("ResourcePicId:"+"["+resourcePic+"]");
//			map.addMarker(new MarkerOptions().position(new LatLng(latitude, longtitude)).icon(BitmapDescriptorFactory.fromResource(resourcePic)).title(message));
//			
//			CameraPosition position=new CameraPosition.Builder().target(new LatLng(latitude, longtitude)).zoom(14).tilt(0).build();
//			map.animateCamera(CameraUpdateFactory.newCameraPosition(position));
			
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		Utils.log("======================set start_map===================");
		/**
		 * 동작이 복잡하지 않는 경우에는 UI Thread 로 대체한다.
		 * 상세한 구현이나 지연시간동안의 작업들이 여러개인 경우에는 Handler 와  Message 구현을 꼭 해준다.
		 */
		new Thread(new Runnable() {
			@Override
			public void run() {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
//						setUpMapIfNeeded();
					}
				});
			}
		}).start();
				
				
		Utils.log("======================endeded start_map===================");
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Utils.log("");
		
	}
	
	@Override
	public void onBackPressed() {

		Utils.showConfirmDialog(MoreGloriaMapPosition.this, "종료",
				"글로리아 모바일을 종료 하시겠습니까?", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						allClearActivity();

					}
				}, null);

	}
//	/**
//	 * Map 에 대한 정보를 가져온다.
//	 * 추가 동작 설명 >> 원래는 내 위치를 가져오 고있었으나 필요없는 것 같아 일단 주석 처리 필요할 시에는
//	 * 주석 만 풀면 된다.
//	 * @return
//	 */
//	private synchronized GoogleMap setUpMapIfNeeded() {
//		if (map == null) {
//			map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.more_map_gloria_view)).getMap();
//			if (map != null) {
////				setUpMap();
//				return map;
//			}
//		}
//		return map;
//	}
//	
//	private void setUpMap() {
//		map.setMyLocationEnabled(true);
//		map.getMyLocation();
//
//	}
	/**
	 * 위치가 변경됐을때의 상황도 필요 없는것 같아서 그냥 PASS
	 */
	@Override
	public void onLocationChanged(Location location) {
		
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		
		
	}

}
